//
//  PictureSelectorViewController.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/31.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "PictureSelectorViewController.h"
#import "PictureSelectorCell.h"

#define column 4
#define pictureCellMaigin 10
#define  maxImageCount 3

@interface PictureSelectorViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, PictureSelectorCellDelegate>

@end

@implementation PictureSelectorViewController

static NSString * const reuseIdentifier = @"Cell";

- (NSMutableArray *)imageList {
    if (_imageList == nil) {
        _imageList = [NSMutableArray arrayWithCapacity:2];
    }
    return _imageList;
}

- (instancetype)init {

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat w = (kScreenWidth - (column + 1) * pictureCellMaigin ) / column;
    layout.itemSize = CGSizeMake(w, w);
    layout.minimumInteritemSpacing = pictureCellMaigin;
    layout.minimumLineSpacing = pictureCellMaigin;
    // 设置 section 间距
    layout.sectionInset = UIEdgeInsetsMake(pictureCellMaigin, pictureCellMaigin, 0, pictureCellMaigin);
    self = [super initWithCollectionViewLayout:layout];
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[PictureSelectorCell class] forCellWithReuseIdentifier:reuseIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger delta = (self.imageList.count == maxImageCount) ? 0 : 1;
    return self.imageList.count + delta;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PictureSelectorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.selectorCellDelegate = self;
    cell.backgroundColor = [[[UIColor alloc] init] randomColor];
    if (indexPath.item == self.imageList.count) {
        cell.image = nil;
    } else {
        cell.image = self.imageList[indexPath.item];
    }
    return cell;
}

// 遵守图片选择器的协议
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
    // 添加图片
    [self.imageList addObject:image];
    // 刷新集合视图
    [self.collectionView reloadData];
    
    // 一旦实现协议方法  dismiss 就交给程序员
    [self dismissViewControllerAnimated:true completion:nil];
}

- (void)userWillAddPicture:(PictureSelectorCell *)cell {
    if (cell.image != nil) {
        return ;
    }
    
    // 使用系统的图片选择器
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    // 指定代理
    picker.delegate = self;
    // 设置是否允许编辑  在上传头像的时候 需要打开
    // picker.allowsEditing = true
    [self presentViewController:picker animated:true completion:nil];
}

- (void)userWillDeletePicture:(PictureSelectorCell *)cell {
    // 获取点击的cell的indexPath  取操作数组
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    [self.imageList removeObjectAtIndex:indexPath.item];
    
    [UIView animateWithDuration:0.5 animations:^{
        cell.alpha = 0;
    } completion:^(BOOL finished) {
        cell.alpha = 1;
        [self.collectionView reloadData];
    }];
}

@end
